/*
 * @Author: song
 * @Description:窗口子孩子绘制基类
 * @Date: 2024年04月23日 星期二 14:04:54
 * @Modify:
 */

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace SCore.Editor.WindowDraw
{
    public abstract class IESubWindow
    {
        [Tooltip("父节点")] public EditorWindow _window;
        [Tooltip("区域")] protected Rect _rect;
        [Tooltip("控件")] private List<ICommonView> _commonViews = new List<ICommonView>();
        [Tooltip("是否按下")] private bool _down;

        public IESubWindow(EditorWindow window)
        {
            _window = window;
        }

        protected virtual List<ICommonView> BindView()
        {
            return new List<ICommonView>();
        }

        //===========Lift
        public virtual void OnGUI()
        {
            Event e = Event.current;
            foreach (var commonView in _commonViews)
            {
                commonView.OnGUI();
            }
        }

        public virtual void UpdateEvent(Event e)
        {
            foreach (var commonView in _commonViews)
            {
                commonView.UpdateEvent(e);
            }

            HandleEvent(e.type, e);
        }

        public virtual void OnEnable()
        {
            _commonViews.AddRange(BindView());
            foreach (var commonView in _commonViews)
            {
                commonView.OnEnable();
            }
        }

        public virtual void OnDisable()
        {
            foreach (var commonView in _commonViews)
            {
                commonView.OnDisable();
            }
        }

        public virtual void Update()
        {
            foreach (var commonView in _commonViews)
            {
                commonView.Update();
            }
        }


        //============>Event
        protected virtual bool MouseDown(Event evt)
        {
            return false;
        }

        protected virtual bool MouseDrag(Event evt)
        {
            return false;
        }

        protected virtual bool MouseWheel(Event evt)
        {
            return false;
        }

        protected virtual bool MouseUp(Event evt)
        {
            return false;
        }

        protected virtual bool DoubleClick(Event evt)
        {
            return false;
        }

        protected virtual bool KeyDown(Event evt)
        {
            return false;
        }

        protected virtual bool KeyUp(Event evt)
        {
            return false;
        }

        protected virtual bool ContextClick(Event evt)
        {
            return false;
        }


        bool HandleEvent(EventType type, Event evt)
        {
            bool isHandled = false;

            switch (type)
            {
                case EventType.ScrollWheel:
                    if (_rect.Contains(evt.mousePosition))
                    {
                        isHandled = MouseWheel(evt);
                    }

                    break;
                case EventType.MouseUp:
                    if (_down)
                    {
                        _down = false;
                        isHandled = MouseUp(evt);
                    }

                    break;
                case EventType.MouseDown:
                    if (_rect.Contains(evt.mousePosition))
                    {
                        _down = true;
                        isHandled = evt.clickCount < 2 ? MouseDown(evt) : DoubleClick(evt);
                    }

                    break;
                case EventType.MouseDrag:
                    if (_down)
                    {
                        isHandled = MouseDrag(evt);
                    }

                    break;
                case EventType.KeyDown:
                    isHandled = KeyDown(evt);
                    break;
                case EventType.KeyUp:
                    isHandled = KeyUp(evt);
                    break;
                case EventType.ContextClick:
                    if (_rect.Contains(evt.mousePosition))
                    {
                        isHandled = ContextClick(evt);
                    }

                    break;
            }

            if (isHandled)
                evt.Use();

            return isHandled;
        }


        protected void DrawTitle(string title)
        {
            GUILayout.Label(title, EditorStyles.boldLabel);
        }

        public Rect GetPosition()
        {
            return _rect;
        }

        public void UpdateArea(Rect rect)
        {
            _rect = rect;
        }

        public T GetWindow<T>() where T : EditorWindow
        {
            return _window as T;
        }
    }
}